<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="43px"
    >
      <el-form-item label="班次" prop="shift">
        <el-select
          v-model="queryParams.shift"
          placeholder="请选择类型"
          style="width: 220px"
        >
          <el-option
            v-for="dict in shifts"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 220px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <div class="searchReset">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </div>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workPlan:workPlan:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workPlan:workPlan:remove']"
          >删除</el-button
        >
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div :class="eleTabTop <= 85 ? 'eleTab' : ''" class="informationTab" ref="eleTab">
      <el-table
        v-loading="loading"
        :data="workPlanList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" width="55" align="center" label="序号" />
        <el-table-column
          label="编号"
          align="center"
          prop="code"
          min-width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="日期"
          align="center"
          prop="jobDate"
          min-width="180"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.jobDate, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="班次"
          align="center"
          prop="shift"
          min-width="100"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.shift == '1'">白班</div>
            <div v-if="scope.row.shift == '2'">夜班</div>
          </template>
        </el-table-column>
        <el-table-column
          label="车间编制人"
          prop="workshopOrganizer"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="车间审核人"
          prop="workshopAuditor"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="编制人"
          prop="preparePeopleName"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="审批人"
          prop="verifyUserName"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleLooks(scope.row)"
              v-hasPermi="['workPlan:workPlan:edit']"
            >
              查看</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['workPlan:workPlan:edit']"
              >编辑</el-button
            >

            <el-dropdown
              size="mini"
              @command="(command) => handleCommand(command, scope.row)"
            >
              <span class="el-dropdown-link">
                <i class="el-icon-d-arrow-right el-icon--right"></i>更多
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  command="handleDelete"
                  icon="el-icon-delete"
                  v-hasPermi="['workPlan:workPlan:remove']"
                  >删除</el-dropdown-item
                >
                <el-dropdown-item
                  command="handlePrinter"
                  icon="el-icon-printer"
                  v-hasPermi="['system:role:edit']"
                  >打印</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
            <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['workPlan:workPlan:remove']"
                >删除</el-button
              >
               <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handlePrinter(scope.row)"
                v-hasPermi="['workPlan:workPlan:remove']"
                >打印</el-button
              > -->
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-dialog
      title="作业内容-打印"
      :visible.sync="openPrinter"
      width="900px"
      append-to-body
    >
      <div style="height: 70vh; overflow-y: scroll; padding-right: 50px">
        <div>
          <div
            style="
              text-align: center;
              font-size: 20px;
              border: 1px solid #dfe6ec;
              height: 50px;
              line-height: 50px;
              border-bottom: none;
            "
          >
            动车组检修作业记录(供电预检)
          </div>

          <div style="display: flex">
            <div
              style="
                border: 1px solid rgb(223, 230, 236);
                height: 221px;
                border-right: none;
              "
            >
              <div
                style="height: 44px; border-bottom: 1px solid rgb(223, 230, 236)"
              ></div>
              <div
                style="
                  height: 175px;
                  line-height: 175px;
                  padding: 0 15px;
                  border-right: 1px solid rgb(223, 230, 236);
                "
              >
                作业时间
              </div>
            </div>
            <el-table
              border
              :data="transData"
              class="coldatass"
              style="margin-bottom: 10px"
              :span-method="arraySpanMethod"
            >
              <el-table-column
                v-for="(item, index) in transTitle"
                :label="item"
                :key="index"
                align="center"
              >
                <template slot-scope="scope">
                  {{ scope.row[index] }}
                </template>
              </el-table-column>
            </el-table>
          </div>

          <!-- <el-table
            border
            :data="transData"
            class="coldatass"
            style="margin-bottom: 10px"
            :span-method="arraySpanMethod"
          >
            <el-table-column
              v-for="(item, index) in transTitle"
              :label="item"
              :key="index"
              align="center"
            >
              <template slot-scope="scope">
                {{ scope.row[index] }}
              </template>
            </el-table-column>
          </el-table> -->
        </div>

        <div>
          <div
            style="
              text-align: center;
              font-size: 20px;
              border: 1px solid #dfe6ec;
              height: 50px;
              line-height: 50px;
              border-bottom: none;
            "
          >
            动车组检修作业记录(断电作业)
          </div>

          <div style="display: flex">
            <div
              style="
                border: 1px solid rgb(223, 230, 236);
                height: 221px;
                border-right: none;
              "
            >
              <div
                style="height: 44px; border-bottom: 1px solid rgb(223, 230, 236)"
              ></div>
              <div
                style="
                  height: 175px;
                  line-height: 175px;
                  padding: 0 15px;
                  border-right: 1px solid rgb(223, 230, 236);
                "
              >
                作业时间
              </div>
            </div>
            <el-table
              border
              :data="PowerOperationList"
              class="coldatass"
              style="margin-bottom: 10px"
            >
              <el-table-column
                v-for="(item, index) in transTitle"
                :label="item"
                :key="index"
                align="center"
              >
                <template slot-scope="scope">
                  {{ scope.row[index] }}
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>

        <div>
          <div
            style="
              text-align: center;
              font-size: 20px;
              border: 1px solid #dfe6ec;
              height: 50px;
              line-height: 50px;
              border-bottom: none;
            "
          >
            动车组检修作业记录(供电检修)
          </div>

          <div style="display: flex">
            <div
              style="
                border: 1px solid rgb(223, 230, 236);
                height: 221px;
                border-right: none;
              "
            >
              <div
                style="height: 44px; border-bottom: 1px solid rgb(223, 230, 236)"
              ></div>
              <div
                style="
                  height: 175px;
                  line-height: 175px;
                  padding: 0 15px;
                  border-right: 1px solid rgb(223, 230, 236);
                "
              >
                作业时间
              </div>
            </div>
            <el-table
              border
              :data="ReconditionList"
              class="coldatass"
              style="margin-bottom: 10px"
            >
              <el-table-column
                v-for="(item, index) in transTitle"
                :label="item"
                :key="index"
                align="center"
              >
                <template slot-scope="scope">
                  {{ scope.row[index] }}
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormPrinter">确 定</el-button>
        <el-button @click="cancelPrinter">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWorkPlan,
  getWorkPlan,
  delWorkPlan,
  addWorkPlan,
  updateWorkPlan,
  printData,
} from "@/api/workPlan/workPlan";

export default {
  name: "WorkPlan",
  data() {
    return {
      originTitle: ["作业开始时间", "安全号志插设人", "作业结束时间", "安全号志撤除人"], // originTitle 该标题为 正常显示的标题, 数组中的顺序就是上面数据源对象中的字段标题对应的顺序
      transTitle: ["检修班组"], // transTitle 该标题为转化后的标题, 注意多一列,  因为原来的标题变成了竖着显示了, 所以多一列标题, 第一个为空即可

      originTitlePowerOperation: [
        "作业开始时间",
        "安全号志插设人",
        "作业结束时间",
        "安全号志撤除人",
      ], // originTitle 该标题为 正常显示的标题, 数组中的顺序就是上面数据源对象中的字段标题对应的顺序
      transTitlePowerOperation: ["检修班组"], // transTitle 该标题为转化后的标题, 注意多一列,  因为原来的标题变成了竖着显示了, 所以多一列标题, 第一个为空即可

      originTitleRecondition: [
        "作业开始时间",
        "安全号志插设人",
        "作业结束时间",
        "安全号志撤除人",
      ], // originTitle 该标题为 正常显示的标题, 数组中的顺序就是上面数据源对象中的字段标题对应的顺序
      transTitleRecondition: ["检修班组"], // transTitle 该标题为转化后的标题, 注意多一列,  因为原来的标题变成了竖着显示了, 所以多一列标题, 第一个为空即可

      // 预检
      transData: [],
      //断电
      PowerOperationList: [],

      // 供电

      ReconditionList: [],
      openPrinter: false,
      shifts: [
        { id: 1, name: "白班" },
        { id: 2, name: "夜班" },
      ],
      dateRange: [],
      eleTabTop: 0, //表格容器eleTabTop距浏览器顶部距离
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检修作业计划表格数据
      workPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        jobDate: null,
        workshopOrganizer: null,
        workshopAuditor: null,
        preparePeopleId: null,
        shift: null,
        deliveryPersonnel: null,
        deliveryPersonnelTel: null,
        maintenance: null,
        attention: null,
        status: null,
        verifyStatus: null,
        verifyDeptId: null,
        verifyUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        delFlag: [{ required: true, message: "删除标志不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    // 滚动条的获取
    this.handleScrollx();
    window.addEventListener("scroll", this.handleScrollx, true);
  },
  activated() {
    //获取表格容器距页面头部的距离
    this.handleScrollx();
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScrollx, true);
  },
  methods: {
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      // console.log(this.transData)
      console.log({ row, column, rowIndex, columnIndex });
      // if (columnIndex === 0) {
      //   return {
      //     rowspan: 5,
      //     colspan: 1,
      //   };
      // }
    },
    submitFormPrinter() {
      this.openPrinter = false;
    },
    cancelPrinter() {
      this.openPrinter = false;
    },
    //打印
    handlePrinter(row) {
      (this.originTitle = [
        "作业开始时间",
        "安全号志插设人",
        "作业结束时间",
        "安全号志撤除人",
      ]), // originTitle 该标题为 正常显示的标题, 数组中的顺序就是上面数据源对象中的字段标题对应的顺序
        (this.transTitle = ["检修班组"]), // transTitle 该标题为转化后的标题, 注意多一列,  因为原来的标题变成了竖着显示了, 所以多一列标题, 第一个为空即可
        (this.originTitlePowerOperation = [
          "作业开始时间",
          "安全号志插设人",
          "作业结束时间",
          "安全号志撤除人",
        ]), // originTitle 该标题为 正常显示的标题, 数组中的顺序就是上面数据源对象中的字段标题对应的顺序
        (this.transTitlePowerOperation = ["检修班组"]), // transTitle 该标题为转化后的标题, 注意多一列,  因为原来的标题变成了竖着显示了, 所以多一列标题, 第一个为空即可
        (this.originTitleRecondition = [
          "作业开始时间",
          "安全号志插设人",
          "作业结束时间",
          "安全号志撤除人",
        ]), // originTitle 该标题为 正常显示的标题, 数组中的顺序就是上面数据源对象中的字段标题对应的顺序
        (this.transTitleRecondition = ["检修班组"]), // transTitle 该标题为转化后的标题, 注意多一列,  因为原来的标题变成了竖着显示了, 所以多一列标题, 第一个为空即可
        // 预检
        (this.transData = []),
        //断电
        (this.PowerOperationList = []),
        // 供电
        (this.ReconditionList = []),
        (this.openPrinter = true);
      // 预检
      let data = [];
      //断电
      let PowerOperation = [];
      // 供电
      let Recondition = [];
      let newData = [];
      let query = {};
      query.workPlanId = 1;

      printData(query).then((res) => {
        // 预检
        data = res.data.previewingList;
        //断电
        (PowerOperation = res.data.powerOperationList),
          // 供电
          (Recondition = res.data.reconditionList);

        data.forEach((element) => {
          let param = {};
          // param.startTime = element.startTime;
          param.startTime =
            element.startTime.substring(11, 13) +
            "时" +
            element.startTime.substring(14, 16) +
            "分";
          param.insertPersonName = element.insertPersonName;
          param.endTime =
            element.endTime.substring(11, 13) +
            "时" +
            element.endTime.substring(14, 16) +
            "分";
          // param.endTime = element.endTime;
          param.removePersonName = element.removePersonName;
          // param.workTeamName = element.workTeamName
          newData.push(param);
          this.transTitle.push(element.workTeamName);
        });
        let matrixData = newData.map((row) => {
          let arr = [];
          for (let key in row) {
            arr.push(row[key]);
          }
          return arr;
        });
        // 加入标题拼接最终的数据
        this.transData = matrixData[0].map((col, i) => {
          return [
            this.originTitle[i],
            ...matrixData.map((row) => {
              return row[i];
            }),
          ];
        });
        // for (let i = 0; i < this.transData.length; i++) {
        //   this.transData[i].unshift("作业时间");
        // }

        let newPowerOperationData = [];
        PowerOperation.forEach((element) => {
          let param = {};
          param.startTime =
            element.startTime.substring(11, 13) +
            "时" +
            element.startTime.substring(14, 16) +
            "分";
          param.insertPersonName = element.insertPersonName;
          param.endTime =
            element.endTime.substring(11, 13) +
            "时" +
            element.endTime.substring(14, 16) +
            "分";
          param.removePersonName = element.removePersonName;
          // param.workTeamName = element.workTeamName
          newPowerOperationData.push(param);
          this.transTitlePowerOperation.push(element.workTeamName);
        });
        let PowerOperationData = newPowerOperationData.map((row) => {
          let arr = [];
          for (let key in row) {
            arr.push(row[key]);
          }
          return arr;
        });
        // 加入标题拼接最终的数据
        this.PowerOperationList = PowerOperationData[0].map((col, i) => {
          return [
            this.originTitlePowerOperation[i],
            ...PowerOperationData.map((row) => {
              return row[i];
            }),
          ];
        });

        // for (let i = 0; i < this.PowerOperationList.length; i++) {
        //   this.PowerOperationList[i].unshift("作业时间");
        // }
        let newReconditionData = [];
        Recondition.forEach((element) => {
          let param = {};
          param.startTime =
            element.startTime.substring(11, 13) +
            "时" +
            element.startTime.substring(14, 16) +
            "分";
          param.insertPersonName = element.insertPersonName;
          param.endTime =
            element.endTime.substring(11, 13) +
            "时" +
            element.endTime.substring(14, 16) +
            "分";
          param.removePersonName = element.removePersonName;
          // param.workTeamName = element.workTeamName
          newReconditionData.push(param);
          this.transTitleRecondition.push(element.workTeamName);
        });
        let ReconditionDatas = newReconditionData.map((row) => {
          let arr = [];
          for (let key in row) {
            arr.push(row[key]);
          }
          return arr;
        });
        // 加入标题拼接最终的数据
        this.ReconditionList = ReconditionDatas[0].map((col, i) => {
          return [
            this.originTitleRecondition[i],
            ...ReconditionDatas.map((row) => {
              return row[i];
            }),
          ];
        });

        // for (let i = 0; i < this.ReconditionList.length; i++) {
        //   this.ReconditionList[i].unshift("作业时间");
        // }
      });
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "handlePrinter":
          this.handlePrinter(row);
          break;
        default:
          break;
      }
    },
    //获取表格容器距页面头部的距离
    handleScrollx() {
      this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top;
    },
    /** 查询检修作业计划列表 */
    getList() {
      this.loading = true;
      listWorkPlan(this.addDateRangeSearch(this.queryParams, this.dateRange)).then(
        (response) => {
          this.workPlanList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        jobDate: null,
        workshopOrganizer: null,
        workshopAuditor: null,
        preparePeopleId: null,
        shift: null,
        deliveryPersonnel: null,
        deliveryPersonnelTel: null,
        maintenance: null,
        attention: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        status: "0",
        verifyStatus: "0",
        verifyDeptId: null,
        verifyUserId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.$router.push({
        path: "/workPlan/workPlan/AddWorkPlan",
      });
    },
    // 查看
    handleLooks(row) {
      this.$router.push({
        name: "AddWorkPlan",
        params: { id: row.id, way: "look" },
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        name: "AddWorkPlan",
        params: { id: row.id, way: "edit" },
      });
      // this.reset();
      // const id = row.id || this.ids;
      // getWorkPlan(id).then((response) => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改检修作业计划";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateWorkPlan(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkPlan(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除检修作业计划编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWorkPlan(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "workPlan/workPlan/export",
        {
          ...this.queryParams,
        },
        `workPlan_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .coldatass .el-table__fixed-header-wrapper th {
  background-color: #fff !important;
}

::v-deep .coldatass .el-table__header-wrapper th,
.coldatass .el-table__fixed-header-wrapper th {
  background-color: #fff !important;
  font-weight: bolder;
}

.coldatass {
  margin-top: 0 !important;
}

::v-deep .coldatass .el-table__header-wrapper,
.coldatass .el-table__fixed-header-wrapper {
  border: none;
}

::v-deep .coldatass .el-table--group,
.el-table--border {
  border-left: none;
}
</style>
